
package util;
import java.sql.Connection;


import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 *
 */
public class Linksql {



    public static Connection getConnection()
    {
        try
        {
            // 加载驱动
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = null;

        try
        {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "20030607");
            System.out.println("数据库连接成功");
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }
        return connection;
    }
    /*
    // 释放资源
    // 关闭结果集 ResultSet
    public void close(ResultSet resultSet) throws Exception {
  if(resultSet != null) {
   resultSet.close();
  }
 }
    
    
    // 关闭 sql 语句对象 Statement
    public void close(Statement statement) throws Exception {
     if(statement != null) {
      statement.close();
     }
    }
*/
    // 释放资源 关闭资源

    public static   void close(ResultSet resultSet, Statement statement, Connection connection)  {

        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }






    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建一个数据库对象
        Linksql dbUtil = new Linksql();
        //连接数据库
        Linksql.getConnection();
    }
    public void closeCon() {
        // TODO Auto-generated method stub

    }

}